<template>
  <div>
    <div class="shebeiliebiao">
      <con-title :topTitle="topTitle"></con-title>
    </div>
    <el-tabs
      type="border-card"
      class="biaoqianye"
      v-model="query.agreement"
      @tab-click="changeTabFun"
    >
      <el-tab-pane label="所有设备" name="all"> </el-tab-pane>
      <el-tab-pane label="LoRaWAN" name="LoRaWAN"></el-tab-pane>
      <el-tab-pane label="LoRaPP" name="LoRaPP"></el-tab-pane>
      <div>
        <el-form label-width="100px">
          <!-- 1 -->
          <el-form-item label="在线状态：">
            <el-select
              placeholder="请选择"
              v-model="query.online"
              style="width: 300px"
            >
              <el-option
                v-for="item in options"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 2 -->
          <el-form-item label="EUl：" style="width: 400px">
            <el-input v-model.trim="query.eui" placeholder="设备EUl"></el-input>
          </el-form-item>
          <!-- 3 -->
          <el-form-item label="绑定时间：" style="width: 400px">
            <el-date-picker
              v-model="query.date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>

            <!-- <el-date-picker
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker> -->
          </el-form-item>
          <!-- 4 -->
          <el-form-item>
            <el-button-group size="mini">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="searchFun"
                >搜索</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-delete"
                size="mini"
                @click="clearFun"
                >清除条件</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-refresh"
                size="mini"
                @click="refreshFun"
                >刷新</el-button
              >
            </el-button-group>
            <span class="jieguo" style="color: #666"
              >搜索结果：{{ total }}</span
            >
          </el-form-item>
        </el-form>
      </div>
    </el-tabs>
    <template>
      <el-table :data="gatewayList" style="width: 98.8%" class="gateway-table">
        <el-table-column type="index" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column prop="eui" label="EUI" width="180" align="center">
        </el-table-column>
        <el-table-column
          prop="name"
          label="设备名称"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="mhz" label="频率（MHz）" align="center">
        </el-table-column>
        <el-table-column label="在线状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.online | onlineType }}
          </template>
        </el-table-column>
        <el-table-column prop="country" label="国家" align="center">
        </el-table-column>
        <el-table-column prop="agreement" label="组网协议类型" align="center">
        </el-table-column>
        <el-table-column label="绑定时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.bTime | time }}
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      class="fenye"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="query.currPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="query.pageSize"
      layout="sizes, prev, pager, next,jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { $gatewayList } from "@/api/index";
import ConTitle from "@/components/ConTitle.vue";
export default {
  name: "gateWay",
  data() {
    return {
      gatewayList: [],
      topTitle: {
        type: "设备",
        text: "网关列表",
        msg: "展示EUI、名称、状态等设备信息，可根据EUI、状态，和绑定时间等条件筛选具体的网关。",
      },
      total: 0,
      query: {
        currPage: 1,
        pageSize: 5,
        eui: "",
        online: "",
        agreement: "all",
        date: [],
      },
      options: [
        { value: true, label: "在线" },
        { value: false, label: "离线" },
        { value: "all", label: "全部" },
      ],
    };
  },
  computed: {
    dateStr() {
      console.log(66);
      let d1 = this.query.date[0];
      let d2 = this.query.date[1];
      //将日期时间转时间戳
      let time1 = d1 && d1.getTime();
      let time2 = d2 && d2.getTime();

      if (time1 && time2) {
        return time1 + "-" + time2;
      } else {
        return "";
      }
    },
  },
  mounted() {
    console.log(this.query);
    $gatewayList().then((res) => {
      console.log(res);
      this.gatewayList = res.data.data;
    });
    this.getGatewayList();
  },
  methods: {
    getGatewayList() {
      console.log(this.query);
      var queryStr = { ...this.query };
      if (queryStr != "") {
        queryStr.date = this.dateStr;
      }
      console.log(queryStr);
      $gatewayList(queryStr).then((res) => {
        console.log(res);
        this.gatewayList = res.data.data;
        this.total = res.data.total;
      });
    },
    handleSizeChange(count) {
      console.log("页面的条数发生变化了", count);
      //将页面显示条数赋值给 pageSize
      this.query.pageSize = count;
      this.query.currPage = 1;
      //重新获取数据
      this.getGatewayList();
    },
    //翻页会触发的函数
    handleCurrentChange(page) {
      console.log("翻页了", page);
      // 修改当前页的页码
      this.query.currPage = page;
      //重新获取数据
      this.getGatewayList();
    },
    searchFun() {
      this.getGatewayList();
    },
    changeTabFun() {
      this.getGatewayList();
    },
    clearFun() {
      this.query = {
        currPage: 1, //页码
        pageSize: 5, //当前页有多少条数据
        eui: "",
        online: "", //是否在线的状态  true 在线
        agreement: "all", //组网协议类型
        date: [], //保存的从表单选择的起始和结束日期
      };
      this.getGatewayList();
    },
    refreshFun() {
      this.$router.go(0);
    },
  },
  filters: {
    onlineType(value) {
      return value ? "在线" : "离线";
    },
    time(val) {
      let d = new Date(+val);
      //   console.log(d);
      return (
        d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日"
      );
    },
  },
  components: {
    ConTitle,
  },
};
</script>

<style lang="scss" scoped>
.gateway-table {
  margin-left: 10px;
}
.shebeiliebiao {
  margin: 10px;
}
.fenye {
  margin-top: 40px;
  text-align: center;
}
.biaoqianye {
  width: 98.8%;
  margin-left: 10px;
  margin-bottom: 10px;
}
.jieguo {
  margin-left: 10px;
}
.iconfont {
  margin-right: 5px;
  font-weight: bold;
}
</style>